package cn.wolfcode.car.business.web.controller.system;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.ServiceItemAuditVO;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 客户控制器
 */
@Controller
@RequestMapping("/business/serviceItem")
public class ServiceItemController {
    //模板前缀
    private static final String prefix = "business/serviceItem/";

    @Autowired
    private IServiceItemService serviceItemService;

    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("serviceItem", serviceItemService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo){

        return serviceItemService.query(qo);
    }


    //新增
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ServiceItem serviceItem){
        ServiceItem newServiceItem = new ServiceItem();
        newServiceItem.setName(serviceItem.getName());
        newServiceItem.setOriginalPrice(serviceItem.getOriginalPrice());
        newServiceItem.setDiscountPrice(serviceItem.getDiscountPrice());
        newServiceItem.setCarPackage(serviceItem.getCarPackage());
        newServiceItem.setInfo(serviceItem.getInfo());
        newServiceItem.setServiceCatalog(serviceItem.getServiceCatalog());
        newServiceItem.setCreateTime(new Date());
        serviceItemService.save(newServiceItem);
        return AjaxResult.success();
    }

    //编辑
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ServiceItem serviceItem){

        serviceItemService.update(serviceItem);
        return AjaxResult.success();
    }

    //删除
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        serviceItemService.deleteBatch(ids);
        return AjaxResult.success();
    }
    @RequestMapping("/saleOn")
    @ResponseBody
    public AjaxResult saleOn(Long id){
        ServiceItem serviceItem = serviceItemService.get(id);
        serviceItem.setSaleStatus(1);
        serviceItemService.updateSaleOn(serviceItem);
        return AjaxResult.success();
    }
    @RequestMapping("/saleOff")
    @ResponseBody
    public AjaxResult saleOff(Long id){
        ServiceItem serviceItem = serviceItemService.get(id);
        serviceItem.setSaleStatus(0);
        serviceItemService.updateSaleOff(serviceItem);
        return AjaxResult.success();
    }

    /**
     * 发起审核
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/startAuditPage")
    public String auditPage(Model model, Long id){
        model.addAttribute("serviceItem", serviceItemService.get(id));
        //部门经理
        model.addAttribute("directors", userService.queryByRoleKey("shopOwner"));
        //财务
        model.addAttribute("finances", userService.queryByRoleKey("financial"));
        //流程
        BpmnInfo bpmnInfo = bpmnInfoService.queryByType("sick-leave");

        model.addAttribute("bpmnInfo", bpmnInfo);
        return  "business/serviceItem/audit";
    }

    /**
     * 启动流程实例
     * @param vo
     * @return
     */
    @RequiresPermissions("business:serviceItem:startAudit")
    @RequestMapping("/startAudit")
    @ResponseBody
    public AjaxResult startAudit(ServiceItemAuditVO vo){
        serviceItemService.startAudit(vo);

        return AjaxResult.success();
    }

}
